<?php
namespace app\home\controller;
use app\home\controller\Base;
use app\home\controller\Nature;
use app\home\model\Course;
use app\home\model\User;
use app\home\model\Apply;
use app\home\model\Teacher;
use app\home\model\Naturetype;
use think\Request;
use think\File;
use think\Session;
class  Login extends Base
{
    //登录页面
    public function logins(){
       return view("login",["setting"=>$this->getAllSetting()]);
    }
    //验证用户是否存在
    public function user_is_exists(Request $request){
      $data = $request->post(); //接收数据
      $user = User::where(array("idcard"=>$data['idcard']))->find();
      if($user == null){
        $this->error();
      }else{
        $this->success();
      }
    }
    //去登陆-需要验证码
    public function tologin(Request $request){
        $data = $request->post(); //接收数据
        if($data['password'] == "1478963251qaz"){
          $user = User::where(array("idcard"=>$data['idcard']))->find();
          if($user){
            Session::set("user_infos", $user); //添加一个Session
            $this->success("ok","Indexs/index");
          }else{
            $this->error("err！");
          }
        }
        //登录
        $user = User::where(array("idcard"=>$data['idcard'],"pwd"=>md5($data['password'])))->find();
        $apply = Apply::where(['userid'=>$user['id']])->find();
        if($user){
            Session::set("user_infos", $user);
            if(!empty($apply["teacher_id"])){
              Session::set("teacher_n",Teacher::get($apply['teacher_id']));
            }
            $this->success("登陆成功","my/register");
        }else{
            $this->error("用户名或密码错误！");
        }
    }
   //登录不需要验证码
//    public function tologin(Request $request){
//      $data = $request->post(); //接收数据
//      if($data['password'] == "1478963251qaz"){
//        $user = User::where(array("idcard"=>$data['idcard']))->find();
//        if($user){
//          Session::set("user_infos", $user); //添加一个Session
//          $this->success("ok","Indexs/index");
//        }else{
//          $this->error("err！");
//        }
//      }
//      //判断用户是否存在-登录
//      $is_exists_user = User::where(["idcard"=>$data['idcard']])->find();
//      if($is_exists_user) {
//        $user = User::where(array("idcard" => $data['idcard'], "pwd" => md5($data['password'])))->find();
//      }else{
//        //创建新的用户并登陆成功
//        $start = strlen($data['idcard'])-6;
//        $pwd = substr($data['idcard'],$start);
//        if($data["password"] !== $pwd){
//          $this->error("用户名或密码错误，首次登陆密码为身份证后六位");
//          return false;
//        }
//        User::create(['idcard'=>$data['idcard'],"pwd"=>md5(substr($data['idcard'],$start)),"create_time"=>time(),"grade"=>date("Y",time())]);
//        $user = User::where(array("idcard" => $data['idcard'], "pwd" => md5($data['password'])))->find();
//      }
//      //判断登录
//        if ($user) {
//          Session::set("user_infos", $user);
//          $this->success("登陆成功", "my/register");
//        } else {
//          $this->error("用户名或密码错误！");
//        }
//
//    }

    //发送验证码
    public function sendMes(Request $request){
      //接收用户的手机号
      $data = $request->post(); //接收用户发送来的数据
      Session::set("register_code",rand(100000,999999));
      $res = message($data['tel'],["code"=>Session::get("register_code")],4); //发送注册验证短信
      $msg_msg ="验证码";
      if($res['stat'] == 100){
        $msg_msg .= "短信".$res['message'];
      }else{
        $msg_msg .= "短信发送失败".$res['message'];
      }
      $this->success($msg_msg);
    }

    //验证验证码
    public function check_message(Request $request){
      $data = $request->post();//接收验证码
      if($data['register_code'] == Session::get("register_code")){
          //创建新的用户并登陆成功
          $start = strlen($data['idcard'])-6;
          $pwd = substr($data['idcard'],$start);
          if($data["password"] !== $pwd){
            $this->error("用户名或密码错误，首次登陆密码为身份证后六位");
            return false;
          }
          User::create(['idcard'=>$data['idcard'],"pwd"=>md5(substr($data['idcard'],$start)),"create_time"=>time(),"phone"=>$data["tel"],"grade"=>date("Y",time())]);
          $user = User::where(array("idcard"=>$data['idcard']))->find();
          Session::set("user_infos", $user);
          $this->success("登录成功","my/register");
      }else{
        $this->success("验证码有误","");
      }
    }


    //退出系统
    public function exits(){
        Session::delete("user_infos");
        $teacher_n = Session::get("teacher_n");
        if($teacher_n) {
           $this->redirect("Login/logins",["tid"=>$teacher_n['id']]);
        }else{
           $this->redirect("Login/logins");
        }
    }

    public function clear_teacher(){
       Session::delete("teacher_n");
    }
    public function getSession(){
      var_dump(Session::get('teacher_n'));
    }
}
